package com.hu.controller;

import com.hu.mapper.MusicListMapping;
import com.hu.mapper.MusicMapping;
import com.hu.pojo.Music;
import com.hu.pojo.MusicList;
import com.hu.utils.RestfulUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.hu.config.LoginHandlerInterceptor.useIdLocal;


@Controller
public class MusicListController {

    @Autowired
    MusicListMapping musicListMapping;

    @GetMapping("/musiclist/getbyid/{musicListId}")
    @ResponseBody
    public String getOne(@PathVariable Integer musicListId){
        MusicList musicList = musicListMapping.quaryById(musicListId);
        return RestfulUtil.toRestful(musicList);
    }

    @PostMapping("/musiclist/getall")
    @ResponseBody
    public String getAll(){
        List<MusicList> musicLists = musicListMapping.queryAll();
        return RestfulUtil.toRestful(musicLists);
    }

    @PostMapping("/musiclist/insert")
    @ResponseBody
    public String insert(MusicList musicList){
        Integer userId = useIdLocal.get();
        if (userId == null){
            return RestfulUtil.toRestful(null,"用户id丢失",404);
        }
        musicList.setCreatUserId(userId.toString());
        musicListMapping.addMusicList(musicList);
        return RestfulUtil.toRestful(null);
    }

    @GetMapping("/musiclist/delete/{musicListId}")
    @ResponseBody
    public String insert(@PathVariable Integer musicListId){
        musicListMapping.deleteMusicList(musicListId);
        return RestfulUtil.toRestful(null);
    }

    //返回当前用户创建的所有歌单
    @PostMapping("/musiclist/getallbyuser")
    @ResponseBody
    public String getAllByUserId(){
        Integer userId = useIdLocal.get();
        if (userId == null){
            return RestfulUtil.toRestful(null,"用户id丢失",404);
        }
        List<MusicList> musicLists = musicListMapping.queryByUser(userId);
        return RestfulUtil.toRestful(musicLists);
    }



}
